package com.easylinkin.linkappapi.rulecomponent.controller;


import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.rulecomponent.entity.RuleComponent;
import com.easylinkin.linkappapi.rulecomponent.service.RuleComponentService;


import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 规则组件前端控制器
 * </p>
 *
 * @author liuming
 * @since 2020-08-27
 */
@RestController
@RequestMapping("/ruleComponent")
public class RuleComponentController {


    @Resource
    private RuleComponentService service;


    @PostMapping("addRuleComponent")
    @CommonOperateLogAnnotate(module = LogModule.RULE_COMPONENT, desc = "新增规则组件")
    public RestMessage addRuleComponent(@RequestBody @Valid RuleComponent ruleComponent) {
        service.addRuleComponent(ruleComponent);
        return RestBuilders.successBuilder().build();
    }
    
    @PostMapping("updateRuleComponent")
    @CommonOperateLogAnnotate(module = LogModule.RULE_COMPONENT, desc = "修改规则组件")
    public RestMessage update(@RequestBody RuleComponent ruleComponent) {
        service.updateRuleComponent(ruleComponent);
        return RestBuilders.successBuilder().build();
    }
    
    @PostMapping("deleteBatchRuleComponent")
    @CommonOperateLogAnnotate(module = LogModule.RULE_COMPONENT, desc = "批量删除规则组件")
    public RestMessage deleteBatchRuleComponent(@RequestBody List<String> idList) {
        Assert.notEmpty(idList, "参数为空");
        service.deleteBatch(idList);
        return RestBuilders.successBuilder().build();
    }
    
    @PostMapping("getPage")
    public RestMessage getRuleComponentsPage(@RequestBody RequestModel<RuleComponent> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<RuleComponent> record = service.getRuleComponents(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    @PostMapping("getRuleComponentById")
    public RestMessage getRuleComponentById(@RequestParam("id") String id) {
        Assert.hasLength(id, "id 不能为空");
        return RestBuilders.successBuilder().data(service.getRuleComponentById(id)).build();
    }

    @PostMapping("getRuleComponent")
    public RestMessage getRuleComponent(@RequestBody RuleComponent ruleComponent) {
        return RestBuilders.successBuilder().data(service.getRuleComponent(ruleComponent)).build();
    }





}
